import os

LOG_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'logs')
os.makedirs(LOG_DIR, exist_ok=True)

LOGGING_CONFIG = {
    'version': 1,
    'disable_existing_loggers': True,
    'loggers': {
        'qcloud_cos': {'level': 'WARNING', 'handlers': ['console'], 'propagate': False},
        'ocr_logger': {'handlers': ['ocr_file'], 'level': 'INFO', 'propagate': False},
    },
    'verbose': {
        'format': '[%(asctime)s] [%(levelname)s] [%(filename)s:%(lineno)d - %(funcName)s] %(message)s'
    },
    'formatters': {
        'default': {'format': '[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S'},
        'verbose': {'format': '[%(asctime)s] %(levelname)s %(filename)s:%(lineno)d - %(message)s'},
    },
    'handlers': {
        'console': {'class': 'logging.StreamHandler', 'formatter': 'default', 'level': 'INFO'},
        'file_info': {
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': os.path.join(LOG_DIR, 'app.log'),
            'when': 'midnight',
            'interval': 1,
            'backupCount': 7,
            'encoding': 'utf-8',
            'formatter': 'verbose',
            'level': 'INFO',
        },
        'file_error': {
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOG_DIR, 'error.log'),
            'maxBytes': 5 * 1024 * 1024,
            'backupCount': 5,
            'encoding': 'utf-8',
            'formatter': 'verbose',
            'level': 'ERROR',
        },
        'ocr_file': {
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOG_DIR, 'ocr_service.log'),
            'maxBytes': 2 * 1024 * 1024,
            'backupCount': 3,
            'encoding': 'utf-8',
            'formatter': 'verbose',
            'level': 'INFO',
        },
    },
    'root': {
        'handlers': ['console', 'file_info', 'file_error'],
        'level': 'INFO',
    },
}


def init_logging():
    import logging.config
    logging.config.dictConfig(LOGGING_CONFIG)
